package net.wofly.cashier.reservation.service.impl;


import net.wofly.cashier.reservation.domain.Reservation;
import net.wofly.cashier.reservation.repository.ReservationRepository;
import net.wofly.cashier.reservation.service.IReservationService;
import net.wofly.cashier.reservation.util.ReservationStatus;
import net.wofly.common.condition.Condition;
import net.wofly.common.condition.ConditionRelate;
import net.wofly.common.condition.FilterCondition;
import net.wofly.common.condition.Operation;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.Identities;
import net.wofly.common.util.PageHelper;
import net.wofly.common.util.ResultCode;
import net.wofly.common.util.ReturnBean;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * 
 * @author liujianbo
 *
 */
@Service("iReservationService")
@Transactional
public class ReservationServiceImpl implements IReservationService {

	@Autowired
	ReservationRepository reservationRepository;

	@Override
	public Reservation save(Reservation entity) {
		return reservationRepository.save(entity);
	}

	@Override
	public void delete(Reservation entity) {
		reservationRepository.delete(entity);
	}

	@Override
	public void delete(String key) {
		reservationRepository.delete(key);
	}

	@Override
	public Reservation findOne(String key) {
		return reservationRepository.findOne(key);
	}

	@Override
	public List<Reservation> save(List<Reservation> entityList) {
		return (List<Reservation>) reservationRepository.save(entityList);
	}

	@Override
	public ReturnBean<Reservation> addReservation(Reservation reservation) {
		reservation.setReservationID(Identities.uuid2());
		reservation.setModifyDate(System.currentTimeMillis());
		reservation.setReservationTime(System.currentTimeMillis());
		return saveReservation(reservation);
	}

	public ReturnBean<Reservation> saveReservation(Reservation reservation) {
		String dayStartTimeStr = DateFormatUtils.format(new Date(reservation.getReservationMealTime()), "yyyy-MM-dd 00:00:00");
		String dayEndTimeStr = DateFormatUtils.format(new Date(reservation.getReservationMealTime()), "yyyy-MM-dd 23:59:59");
		try {
			Date dayStartTime = DateUtils.parseDate(dayStartTimeStr, "yyyy-MM-dd 00:00:00");
			Date dayEndTime = DateUtils.parseDate(dayEndTimeStr, "yyyy-MM-dd 23:59:59");
			Integer reservationNo = reservationRepository.findByReservationTimeBetween(dayStartTime.getTime(), dayEndTime.getTime());
			if (reservationNo == null) {
				reservation.setReservationNo(1);
			} else {
				reservation.setReservationNo(reservationNo + 1);
			}

			return new ReturnBean<Reservation>(ResultCode.SUCCESS, "新增预约成功！", reservationRepository.save(reservation));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new ReturnBean<Reservation>(ResultCode.ERROR_SERVICE, "业务数据错误！");
	}

	@Override
	public ReturnBean<String> cancelReservation(String reservationID, String employeeUserID) {
		Reservation reservation = reservationRepository.findOne(reservationID);
		if (reservation == null) {
			return new ReturnBean<String>(ResultCode.ERROR_SERVICE, "预约信息不存在！");
		}

		reservation.setStatus(ReservationStatus.取消.getValue());
		reservation.setModifyEmployeeUserID(employeeUserID);
		reservation.setModifyDate(System.currentTimeMillis());
		reservationRepository.save(reservation);

		return new ReturnBean<String>(ResultCode.SUCCESS, "取消预约成功！");
	}

	@Override
	public ReturnBean<PageWrapper<Reservation>> getAllReservation(Integer pageNum, Integer pageSize, String employeeUserID, Long reservationMealTime, Integer reservationMealType) {

		Condition condition = new Condition();
		List<FilterCondition> filterConditionList = new ArrayList<FilterCondition>();
		if (reservationMealTime != null && reservationMealTime != -1) {
			String dayStartTimeStr = DateFormatUtils.format(new Date(reservationMealTime), "yyyy-MM-dd 00:00:00");
			String dayEndTimeStr = DateFormatUtils.format(new Date(reservationMealTime), "yyyy-MM-dd 23:59:59");

			try {
				Date dayStartTime = DateUtils.parseDate(dayStartTimeStr, "yyyy-MM-dd 00:00:00");
				Date dayEndTime = DateUtils.parseDate(dayEndTimeStr, "yyyy-MM-dd 23:59:59");
				filterConditionList.add(new FilterCondition(String.valueOf(dayStartTime.getTime()), "reservationMealTime", ConditionRelate.and.name(), Operation.ge.name(), 1));
				filterConditionList.add(new FilterCondition(String.valueOf(dayEndTime.getTime()), "reservationMealTime", ConditionRelate.and.name(), Operation.le.name(), 1));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		if (reservationMealType != null && reservationMealType != -1) {
			filterConditionList.add(new FilterCondition(String.valueOf(reservationMealType), "reservationMealType", ConditionRelate.and.name(), Operation.eq.name(), 2));
		}

		condition.setFilterCondition(filterConditionList);
		// 获得分页对象pageable，并且在pageable中页码是从0开始，设定按照sortType升序排列
		Pageable pageable = new PageRequest(pageNum - 1, pageSize);
		Page<Reservation> page = reservationRepository.findAll(condition.getSpecification(), pageable);
		return new ReturnBean<PageWrapper<Reservation>>(ResultCode.SUCCESS, "获取所有预约记录成功！", PageHelper.toPage(page));
	}

	@Override
	public ReturnBean<String> repast(String reservationID, String employeeUserID) {
		Reservation reservation = reservationRepository.findOne(reservationID);
		if (reservation == null) {
			return new ReturnBean<String>(ResultCode.ERROR_SERVICE, "预约信息不存在！");
		}

		reservation.setStatus(ReservationStatus.就餐.getValue());
		reservation.setModifyEmployeeUserID(employeeUserID);
		reservation.setModifyDate(System.currentTimeMillis());
		reservationRepository.save(reservation);

		return new ReturnBean<String>(ResultCode.SUCCESS, "预约信息置为就餐成功！");
	}

	@Override
	public ReturnBean<Reservation> updateReservation(Reservation reservation) {

		Reservation updateReservation = reservationRepository.findOne(reservation.getReservationID());
		if (updateReservation == null) {
			return new ReturnBean<Reservation>(ResultCode.ERROR_SERVICE, "业务数据错误！");
		}

		reservation.setReservationID(updateReservation.getReservationID());
		reservation.setModifyDate(updateReservation.getModifyDate());
		reservation.setReservationTime(updateReservation.getReservationTime());
		ReturnBean<Reservation> returnBean = this.saveReservation(reservation);
		returnBean.setMsg("修改预约成功！");
		return returnBean;
	}
	
	@Override
	public Reservation findByTableIDAndStatusAndReservationMealTimeBetween(
			String tableID, Integer status, Long startTime, Long endTime) {
        return reservationRepository.findByTableIDAndStatusAndReservationMealTimeBetween(tableID, status, startTime,
                endTime);
    }
}
